<template>
  <div id="QuestionsView">
    <a-form :model="searchParams" :layout="'inline'">
      <a-form-item field="title" label="名称" style="min-width: 200px">
        <a-input
          v-model="searchParams.title"
          placeholder="请输入题目名称"
          allow-clear
        />
      </a-form-item>
      <a-form-item field="tag" label="标签" style="min-width: 240px">
        <a-input-tag
          v-model="searchParams.tags"
          placeholder="请输入题目标签"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSearch">搜 索</a-button>
      </a-form-item>
    </a-form>
    <a-divider direction="horizontal" size="0px" />
    <a-table
      :columns="columns"
      :data="dataList"
      :stripe="true"
      :table-layout-fixed="true"
      :loading="isLoading"
      :pagination="{
        total: total,
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
      }"
      @page-change="onPageChange"
    >
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag v-for="(tag, index) of record.tags" :key="index" color="purple"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #acceptedRate="{ record }">
        <a-space>
          {{
            `${
              record.submitNum
                ? ((record.acceptedNum / record.submitNum) * 100).toFixed(2)
                : "0.00"
            }% (${record.acceptedNum}/${record.submitNum})`
          }}
        </a-space>
      </template>

      <template #createTime="{ record }">
        {{ moment(record.createTime).format("yyyy-MM-DD") }}
      </template>
      <template #optional="{ record }">
        <a-button type="primary" @click="toQuestionPage(record)" title="做题"
          >做题
        </a-button>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref, watchEffect } from "vue";
import {
  Question,
  QuestionControllerService,
  QuestionQueryRequest,
} from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import moment from "moment";
let isLoading = true;
const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionQueryRequest>({
  pageSize: 10,
  current: 1,
  title: "",
  tags: [],
});
const loadData = async () => {
  isLoading = true;
  const res = await QuestionControllerService.listQuestionVoByPageUsingPost(
    searchParams.value
  );
  if (res.code === 0) {
    dataList.value = res.data.records;
    total.value = res.data.total;
    console.log(dataList.value);
    isLoading = false;
  } else {
    message.error("查询失败," + res.message);
    isLoading = false;
  }
  isLoading = false;
};
onMounted(() => {
  loadData();
});

const columns = [
  {
    title: "题号",
    dataIndex: "id",
  },
  {
    title: "标题",
    dataIndex: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过率",
    slotName: "acceptedRate",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    title: "做题",
    align: "center",
    slotName: "optional",
  },
];
const router = useRouter();
const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};
watchEffect(() => {
  loadData();
});
/**
 * 跳转到做题页面
 * @param question
 */
const toQuestionPage = (question: Question) => {
  router.push({
    path: `/views/question/${question.id}`,
  });
};
const doSearch = () => {
  // 重置搜索页号
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  loadData();
};
</script>

<style scoped>
#QuestionsView {
  max-width: 1280px;
  margin: 0 auto;
}
</style>
